<?php
include_once ('class.mysql.php');

class RegisterOrder
{
    private $comtec_id_machine;
    private $comtec_id_responsible;
    private $operation_type;
    private $notes;
    private $tasks;
    private $cod_order;
    private $status_reg;
    private $order_start_date;
    private $order_end_date;
    private $errors;
    private $token;

    public function __construct()
    {
     $this->errors = array();
     $this->comtec_id_mainteiner = $_POST['comtec_id_mainteiner'];
     $this->comtec_id_machine = $_POST['comtec_id_machine'];
     $this->cod_order = $_POST['cod_order'];
     $this->comtec_id_responsible = $_POST['comtec_id_responsible'];
     $this->operation_type = $_POST['operation_type'];
     $this->order_start_date = $_POST['op_year1'].$_POST['op_month1'].$_POST['op_day1'];
     $this->order_end_date = $_POST['op_year2'].$_POST['op_month2'].$_POST['op_day2'];
     $this->tasks = $_POST['tasks'];
     $this->notes = $_POST['notes'];
     if(strlen($this->notes) > 400)
     {
      $this->notes = substr($this->notes,0,400);   
     } 
     $this->status_reg	= 1;
     $this->token = $_POST['token'];
    }

    public function process()
    {
	$this->register();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function register()
    {
        $db = new MySQL();

        $stmt = $db->query("INSERT INTO orders (
                                comtec_id_mainteiner,
                                comtec_id_machine,
                                comtec_id_responsible,
                                operation_type,
                                cod_order,
                                tasks,
                                notes,                               
                                order_start_date,
                                order_end_date,                              
                                status_reg
                           ) VALUES (
                                 $this->comtec_id_mainteiner,
                                 $this->comtec_id_machine,
                                 $this->comtec_id_responsible,
                                 $this->operation_type,
                                '$this->cod_order',
                                '$this->tasks',                              
                                '$this->notes',                          
                                '$this->order_start_date',
                                '$this->order_end_date',                               
                                 $this->status_reg
                           )");

        if($db->affected_rows() < 1)
            $this->errors[] = 'No se puede procesar la inscripci&oacute;n';
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
